package com.samphanie.ane.ui.tab.bottom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.samphanie.ane.library.util.AneDisplayUtil;
import com.samphanie.ane.ui.R;
import com.samphanie.ane.ui.common.IAneTabLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author ZSY
 * Tips:
 * 1. 透明度和底部透出，列表可渲染高度问题
 * 2. 中间高度超过，凸起布局
 */
public class AneTabBottomLayout extends FrameLayout implements IAneTabLayout<AneTabBottom, AneTabBottomInfo<?>> {

    private final List<OnTabSelectedListener<AneTabBottomInfo<?>>> tabSelectedChangeListeners = new ArrayList<>();
    private AneTabBottomInfo<?> selectedInfo;
    private float bottomAlpha = 1f;
    //TabBottom高度
    private static float tabBottomHeight = 50;
    //TabBottom的头部线条高度
    private float bottomLineHeight = 0.5f;
    //TabBottom的头部线条颜色
    private String bottomLineColor = "#dfe0e1";
    private List<AneTabBottomInfo<?>> infoList;

    public AneTabBottomLayout(@NonNull Context context) {
        this(context, null);
    }

    public AneTabBottomLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AneTabBottomLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public AneTabBottom findTab(@NonNull AneTabBottomInfo<?> info) {

        ViewGroup ll = findViewWithTag(TAG_TAB_BOTTOM);
        for (int i = 0; i < ll.getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof AneTabBottom) {
                AneTabBottom tab = (AneTabBottom) child;

                if (tab.getTabInfo() == info) {
                    return tab;
                }
            }
        }

        return null;
    }

    @Override
    public void addTabSelectedChangeListener(OnTabSelectedListener<AneTabBottomInfo<?>> listener) {
        tabSelectedChangeListeners.add(listener);
    }

    @Override
    public void defaultSelected(@NonNull AneTabBottomInfo<?> defaultInfo) {
        onSelected(defaultInfo);
    }

    private static final String TAG_TAB_BOTTOM = "TAG_TAB_BOTTOM";

    @Override
    public void inflateInfo(@NonNull List<AneTabBottomInfo<?>> infoList) {

        if (infoList.isEmpty()) {
            return;
        }

        this.infoList = infoList;
        // 移除之前已经添加的View
        for (int i = getChildCount() - 1; i > 0; i--) {
            removeViewAt(i);
        }

        selectedInfo = null;
        addBackground();
        // 清除之前添加的HiTabBottom listener，Tips：Java foreach remove问题
        Iterator<OnTabSelectedListener<AneTabBottomInfo<?>>> iterator = tabSelectedChangeListeners.iterator();
        while (iterator.hasNext()) {
            if (iterator.next() instanceof AneTabBottom) {
                iterator.remove();
            }
        }

        FrameLayout ll = new FrameLayout(getContext());
        ll.setTag(TAG_TAB_BOTTOM);
        int height = AneDisplayUtil.dp2px(tabBottomHeight, getResources());
        int width = AneDisplayUtil.getDisplayWidthInPx(getContext()) / infoList.size();
        for (int i = 0; i < infoList.size(); i++) {
            AneTabBottomInfo<?> info = infoList.get(i);

            // Tips； 为何不使用LinearLayout： 动态改变child大小后 Gravity.BOTTOM 会失效
            LayoutParams params = new LayoutParams(width, height);
            params.gravity = Gravity.BOTTOM;
            params.leftMargin = i * width;

            AneTabBottom tabBottom = new AneTabBottom(getContext());
            tabSelectedChangeListeners.add(tabBottom);
            tabBottom.setAneTabInfo(info);
            ll.addView(tabBottom, params);

            tabBottom.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSelected(info);
                }
            });
        }

        // 添加到父布局
        LayoutParams flParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        flParams.gravity = Gravity.BOTTOM;
        addBottomLine();
        addView(ll, flParams);
    }

    // 添加分割线
    private void addBottomLine() {
        View bottomLine = new View(getContext());
        bottomLine.setBackgroundColor(Color.parseColor(bottomLineColor));

        LayoutParams bottomLineParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AneDisplayUtil.dp2px(bottomLineHeight, getResources()));
        bottomLineParams.gravity = Gravity.BOTTOM;
        bottomLineParams.bottomMargin = AneDisplayUtil.dp2px(tabBottomHeight - bottomLineHeight, getResources());
        addView(bottomLine, bottomLineParams);
        bottomLine.setAlpha(bottomAlpha);

    }

    private void onSelected(@NonNull AneTabBottomInfo<?> nextInfo) {
        for (OnTabSelectedListener<AneTabBottomInfo<?>> listener : tabSelectedChangeListeners) {
            listener.onTabSelectedChange(infoList.indexOf(nextInfo), selectedInfo, nextInfo);
        }
    }

    private void addBackground() {
        Context context = getContext();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.ane_bottom_layout_bg, null);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AneDisplayUtil.dp2px(tabBottomHeight, getResources()));
        params.gravity = Gravity.BOTTOM;
        view.setAlpha(bottomAlpha);
    }

    public void setTabAlpha(float alpha) {
        this.bottomAlpha = alpha;
    }

    public void setTabHeight(float tabHeight) {
       tabBottomHeight = tabHeight;
    }

    public void setBottomLineHeight(float bottomLineHeight) {
        this.bottomLineHeight = bottomLineHeight;
    }

    public void setBottomLineColor(String bottomLineColor) {
        this.bottomLineColor = bottomLineColor;
    }
}
